package com.canbot.u05.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;

import java.util.List;

/**
 * Created by X230 on 2017/6/9.
 */

public class AttendanceExcelAdapter extends BaseAdapter{

        private Context context;

        private List<String> data;

        private LayoutInflater inflater;

        private boolean isShowDelete;


        private OnDeleteClickListener mListener;

        public AttendanceExcelAdapter(Context context, List data) {
                this.context = context;
                this.data = data;
                inflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup viewGroup) {
                ViewHolder holder = null;
                if (convertView == null) {
                        holder = new ViewHolder();
                        convertView = inflater.inflate(R.layout.layout_attendance_table, null);
                        holder.fileName = (TextView) convertView.findViewById(R.id.layout_item_tv_excel);
                        holder.imageView = (ImageView) convertView.findViewById(R.id.layout_item_iv_excel);
                        holder.iconDel = (Button) convertView.findViewById(R.id.id_imv_dele);

                        convertView.setTag(holder);
                }
                else {
                        holder = (ViewHolder) convertView.getTag();
                }
                String s = data.get(position);
                Log.e("t", "" + s);
                holder.fileName.setText(s + ".xls");
                holder.imageView.setImageResource(R.drawable.selector_excel);
                holder.iconDel.setVisibility(isShowDelete?View.VISIBLE:View.GONE);

                if(holder.iconDel.getVisibility()==View.VISIBLE) {
                       holder.iconDel.setOnClickListener(new View.OnClickListener() {
                               @Override
                               public void onClick(View v) {
                                     mListener.onDelete(position);
                               }
                       });
                }
                return convertView;
        }

        public void setShowDelete(boolean isShowDelete){
                this.isShowDelete=isShowDelete;
                notifyDataSetChanged();
        }
        protected boolean getShowDelete(){
                return isShowDelete;
        }

        public void setOnDelteListener(OnDeleteClickListener l){
                this.mListener = l;
        }



        private class ViewHolder {

                private TextView fileName;

                private ImageView imageView;

                private Button iconDel;
        }

        public interface OnDeleteClickListener{
             void  onDelete(int index);
        }

        @Override
        public int getCount() {
                return data.size();
        }

        @Override
        public Object getItem(int i) {
                return data.get(i);
        }

        @Override
        public long getItemId(int i) {
                return i;
        }
}
